//
//  NavigationHeadView.m
//  at_xiaoge_client
//
//  Created by Linyu Niu on 15/03/2018.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//


#import "NavigationHeadView.h"

@interface NavigationHeadView(){
    NSDictionary * params;
}
@end

@implementation NavigationHeadView

-(instancetype)initWithFrame:(CGRect)frame params:(NSDictionary *)dic{
    self = [super initWithFrame:frame];
    params = dic;
    [self setUpHeadView];
    return self;
}
-(void)setUpHeadView{
    self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 85);

    self.backgroundColor = [UIColor colorWithRed:30.0 / 255.0 green:54.0 / 255.0 blue:71.0 / 255.0 alpha:1.0];
    UILabel * titleLabel = [[UILabel alloc] init];
    
    titleLabel.frame = CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height, self.frame.size.width, self.frame.size.height - [UIApplication sharedApplication].statusBarFrame.size.height);
    titleLabel.text = [params objectForKey:@"title"];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];//[UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
    [self addSubview:titleLabel];
    
    
    
    /*
     *
     leftTitle-左边按钮名字
     leftBtnImage-左边按钮图片
     rightBtnImage-左边按钮图片
     rightTitle-右边按钮名字
     */
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.tag = 0x0091;
    backButton.titleLabel.font = [UIFont systemFontOfSize:15];
    backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    backButton.frame = CGRectMake(15, titleLabel.frame.origin.y, 40, titleLabel.frame.size.height);
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"home_back"] forState:UIControlStateNormal];
    
    if ([params objectForKey:@"leftTitle"]) {
        [backButton setTitle:[params objectForKey:@"leftTitle"] forState:UIControlStateNormal];
    }
    if ([params objectForKey:@"leftBtnImage"]) {
        [backButton setImage:[UIImage imageNamed:[params objectForKey:@"leftBtnImage"]] forState:UIControlStateNormal];
    }
    
    [self addSubview:backButton];
    
    
    UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.tag = 1;
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    rightButton.frame = CGRectMake(self.frame.size.width - backButton.frame.size.width - 15, titleLabel.frame.origin.y, backButton.frame.size.width, titleLabel.frame.size.height);
    [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    
    if ([params objectForKey:@"rightTitle"]) {
        [rightButton setTitle:[params objectForKey:@"rightTitle"] forState:UIControlStateNormal];
    }
    if ([params objectForKey:@"rightBtnImage"]) {
        [rightButton setImage:[UIImage imageNamed:[params objectForKey:@"rightBtnImage"]] forState:UIControlStateNormal];
    }
    if ([params objectForKey:@"rightTextColor"]) {
        [rightButton setTitleColor:[params objectForKey:@"rightTextColor"] forState:UIControlStateNormal];
    }
    
    [self addSubview:rightButton];
    
    

    
//    UIView * lineView = [[CTMediator sharedInstance] lineView:CGRectMake(0, self.frame.size.height - 1.0, self.frame.size.width, 1.0)];
//
//    [self addSubview:lineView];
    
}

-(void)rightAction:(UIButton *)button{
    void(^rightBlock)(UIButton * sender) = [params objectForKey:@"rightAction"];
    if (rightBlock) {
        rightBlock(button);
    }
}
    -(UIViewController *)findViewController:(UIView*)view
    {
        id responder = view;
        while (responder){
            if ([responder isKindOfClass:[UIViewController class]]){
                return responder;
            }
            responder = [responder nextResponder];
        }
        return nil;
    }
    
-(void)backAction:(UIButton *)sender{
    
    void(^leftAction)(UIButton * button) = [params objectForKey:@"leftAction"];
    if (leftAction) {
        leftAction(sender);
    }
//    [self removeFromSuperview];
}
@end
